import java.util.Arrays;

public class Solution{
    public static void main(String[] args){
        int[] nums = {-1,2,1,-4};
        System.out.println(new Solution().threeSumClosest(nums,1));
    }

    public int threeSumClosest(int[] nums, int target) {
        int result = nums[0] + nums[1] + nums[nums.length - 1];
        Arrays.sort(nums);
        for(int i=0; i<nums.length-2;i++){
            int start=i+1;
            int end =nums.length-1;
            while(start<end){
                int sum = nums[i] + nums[start] +nums[end];
                if (sum > target){
                    end--;
                }else {
                    start++;
                }
                if (Math.abs(sum-target)<Math.abs(result-target)){
                    result =sum;
                }
            }
        }
        return result;
    }
}